package com.miner;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.miner.updatedemo.R;
import com.miner.updatedemo.service.DownloadAPKService;

import static com.miner.MainActivity.UPDATE_URL_EXTRA;
import static com.miner.updatedemo.service.DownloadAPKService.PROGRESS_VALUE_EXTRA;

public class UpdateActivity extends AppCompatActivity {
    private static final String LOG_TAG = AppCompatActivity.class.getName();
    public static final String UPDATE_WAITING_ACTION = "update_waiting_action";
    public static final String UPDATE_PROGRESS_ACTION = "update_progress_action";
    public static final String UPDATE_COMPLETE_ACTION = "update_complete_action";
    public static final String UPDATE_FAILED_ACTION = "update_failed_action";
    private UpdateProgressBroadcast mBroadcast;
    private ProgressBar mPb;
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update);
        mPb = (ProgressBar) findViewById(R.id.pb);
        mTextView = (TextView) findViewById(R.id.proText);
        mBroadcast = new UpdateProgressBroadcast();
        Intent it = getIntent();
        String mUrl = it.getStringExtra(UPDATE_URL_EXTRA);
        boolean start = it.getBooleanExtra(MainActivity.START_DOWNLOAD_EXTRA, false);

        IntentFilter filter = new IntentFilter();
        filter.addAction(UPDATE_WAITING_ACTION);
        filter.addAction(UPDATE_PROGRESS_ACTION);
        filter.addAction(UPDATE_COMPLETE_ACTION);
        filter.addAction(UPDATE_FAILED_ACTION);
        registerReceiver(mBroadcast, filter);

        if (!start)
            startDownloadAPK(mUrl);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopDownloadService();
        unregisterReceiver(mBroadcast);
    }

    private void startDownloadAPK(String url) {
        Log.e(LOG_TAG, url);
        startDownloadService(url);
    }

    private void startDownloadService(String url) {
        Intent it = new Intent(this, DownloadAPKService.class);
        it.putExtra(UPDATE_URL_EXTRA, url);
        startService(it);
    }

    private void stopDownloadService() {
        stopService(new Intent(UpdateActivity.this, DownloadAPKService.class));
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        finish();
        return super.onKeyDown(keyCode, event);
    }

    public class UpdateProgressBroadcast extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action) {
                case UPDATE_WAITING_ACTION:
                    mTextView.setText("Waiting..");
                    break;
                case UPDATE_PROGRESS_ACTION:
                    int value = ((int) intent.getDoubleExtra(PROGRESS_VALUE_EXTRA, 0));
                    //更新进度条
                    if (mPb != null) {
                        mPb.setProgress(value);
                    }
                    mTextView.setText(value + "%");
                    break;
                case UPDATE_COMPLETE_ACTION:
                    mTextView.setText("下载完成！");
                    break;
                case UPDATE_FAILED_ACTION:
                    mTextView.setText("下载失败！");
                    break;
            }
        }
    }
}
